let indexPageUri = "/tattoo/index.html";

/**
 * 获取url参数
 * @param name
 * @returns {string|null}
 */
function getUrlParam(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    let r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

$(".login-button").click(function () {
    let username = $("#username").val();
    let password = $("#password").val();
    let loginURL = $(".login-button").attr("login-url");
    $.ajax({
        type: 'POST',
        url: loginURL,
        data: {
            username: username,
            password: password
        },
        success: function (data) {
            let token = data.token;
            localStorage.setItem("token", token);
            let currentTime = new Date().getTime();
            localStorage.setItem("getTokenTime", currentTime.toString());
            localStorage.setItem("tokenExpiration", data.expirationTime.toString());

            let redirect = getUrlParam("redirect");
            if (redirect != null) {
                location.href = decodeURIComponent(getUrlParam("redirect"));
            } else {
                location.href = indexPageUri;
            }
        },
        error: function (data) {
            console.log(data);
        }
    });
});